package yxy.game.pm2.command;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.message.MainMessage;
import yxy.game.pm2.module.stat.StatHandle;

/** 综合 新手引导存储 */
@MessageType(proto = Command.GuideSave, classtype = MainMessage.GuideSaveRequest.class)
public class CMD_GuideSave extends Command<MainMessage.GuideSaveRequest> {

	@Override
	public void execute(MessageSource messageSource, MainMessage.GuideSaveRequest request) {
		String playerCode = messageSource.getTag();
		String guide = request.getGuide();

		MainMessage.GuideSaveResponse.Builder response = MainMessage.GuideSaveResponse.newBuilder();

		PlayerBean playerBean = PlayerBean.get(playerCode);
		try {
			if (playerBean.tryLock()) {
				playerBean.setGuide(guide);
				playerBean.set();
				send(messageSource, response, Result.Success);
				
				StatHandle.stat_新手引导(playerBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			playerBean.unlock();
		}

	}

	private void send(MessageSource messageSource, MainMessage.GuideSaveResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}